package org.dromara.business.modules.merchant.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.modules.merchant.domain.bo.BuMerchantBo;
import org.dromara.business.modules.merchant.domain.vo.BuMerchantVo;
import org.dromara.business.modules.merchant.service.IBuMerchantService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.AuditBo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商户信息
 *
 * @author ckk
 * @date 2025-11-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/merchant")
public class BuMerchantController extends BaseController {

    private final IBuMerchantService buMerchantService;

    /**
     * 判断是否为商户
     *
     * @return {@link Integer 0-已审核，1-待审核,-1-未注册}
     */
    @GetMapping("/isMerchant")
    public R<Integer> isMerchant() {
        return R.ok(buMerchantService.isMerchant());
    }

    /**
     * 我的商户信息
     */
    @GetMapping("/myMerchantInfo")
    public R<BuMerchantVo> myMerchantInfo() {
        return R.ok(buMerchantService.merchantInfo(LoginHelper.getUserId()));
    }

    /**
     * 查询商户信息列表
     */
    @SaCheckPermission("business:merchant:list")
    @GetMapping("/list")
    public TableDataInfo<BuMerchantVo> list(BuMerchantBo bo, PageQuery pageQuery) {
        return buMerchantService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商户信息列表
     */
    @SaCheckPermission("business:merchant:export")
    @Log(title = "商户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BuMerchantBo bo, HttpServletResponse response) {
        List<BuMerchantVo> list = buMerchantService.queryList(bo);
        ExcelUtil.exportExcel(list, "商户信息", BuMerchantVo.class, response);
    }

    /**
     * 获取商户信息详细信息
     *
     * @param id 主键
     */
    // @SaCheckPermission("business:merchant:query")
    @GetMapping("/{id}")
    public R<BuMerchantVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(buMerchantService.queryById(id));
    }

    /**
     * 新增商户信息
     */
    // @SaCheckPermission("business:merchant:add")
    @Log(title = "商户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BuMerchantBo bo) {
        return toAjax(buMerchantService.insertByBo(bo));
    }

    /**
     * 修改商户信息
     */
    // @SaCheckPermission("business:merchant:edit")
    @Log(title = "商户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BuMerchantBo bo) {
        return toAjax(buMerchantService.updateByBo(bo));
    }

    /**
     * 删除商户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:merchant:remove")
    @Log(title = "商户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(buMerchantService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量审核商户信息
     */
    @SaCheckPermission("business:merchant:audit")
    @Log(title = "商户信息", businessType = BusinessType.AUDIT)
    @PostMapping("/batchAudit")
    public R<Void> batchAudit(@RequestBody AuditBo bo) {
        return toAjax(buMerchantService.auditByIds(bo));
    }
}
